See Result Configuration for basic information about how results are configuration.

Overview

Result Types are classes that determine what happens after an Action executes and a Result is returned. Developers are free to create their own Result Types according to the needs of their application or environment. In WebWork 2 for example, Servlet and Velocity Result Types have been created to handle rendering views in web applications.

Note: All built in webwork result types implement the com.opensymphony.xwork.Result interface, which represents a generic interface for all action execution results, whether that be displaying a webpage, generating an email, sending a JMS message, etc.

Result types define classes and map them to names to be referred in the action configuration results. This serves as a shorthand name-value pair for these classes.

snippet of webwork-default.xml
...
<result-types>
    <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
    <result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
    <result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
    <result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/>
    <result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/>
    <result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/>
    <result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/>
    <result-type name="httpheader" class="com.opensymphony.webwork.dispatcher.HttpHeaderResult"/>
    <result-type name="stream" class="com.opensymphony.webwork.dispatcher.StreamResult"/>
    <result-type name="plaintext" class="com.opensymphony.webwork.dispatcher.PlainTextResult" />
</result-types>
...
snippet of your xwork.xml
<include file="webwork-default.xml"/>

<package name="myPackage" extends="default">
  <action name="bar" class="myPackage.barAction">
    <!-- default result type is "dispatcher" -->
    <!-- default result name is "success" -->
    <result>foo.jsp</result>
    <result name="error">error.jsp</result>
    </result>
  </action>
</package>

Writing Custom Results

To write a custom Result, one would need to either

  • implements com.opensymphony.xwork.Result interface
  • extends com.opensymphony.webwork.dispatcher.WebWorkResultSupport

Implementing Result interface

When writing Web based application that uses WebWork and XWork, it is adviced to extends custom Result from WebWorkResultSupport in favour of implementing Result interface as there are quite a bit of functionality that WebWork could recognized from the former approach.

Result interface is pretty straight forward with just the following method to be implemented

public void execute(ActionInvocation invocation) throws Exception;

The above execute(...) method is called when a WebWork action is being executed, a custom implementation could do thing like eg. sending an email, sending a JMS message, rendering some Swing component or just dispatching to a JSP page.

Various information could be obtained from the ActionInvocation object passed in as the argument.

Information Method
WebWork's Action Object actionInvocation.getAction()
Has Action Invocation being executed before actionInvocation.isExecuted()
WebWork's Action context actionInvocation.getInvocationContext()
WebWork's Action Proxy actionInvocation.getProxy()
WebWork's Ognl Value Stack actionInvocation.getStack()

Extending WebWorkResultSupport

WebWorkResultSupport is a support Result class that implements Result interface and add on various convenience methods that might prove useful to its subclass. The information regarding Result interface applies to WebWorkResultSupport as well. WebWorkSupportResult adds the following functionality:-

location property and its relevant accessors

With this property present, WebWork will be able to determine the location of a result. eg. instead of writting xwork's xml descriptor like this

<action ...>
   <result ...>
     <param name="location">xxxx</param>
   </result>
 </action>

one could just do
<action ...>
<result ...>xxxx</result>
</action>
as without the present of <param> tag, the content of the <result> tag will be taken as the location property.

parse property and its relevant accessors

WebWorkResultSupport added a parse property which by default is true, which could be disabled by eg.

<action ...>
   <result ...>
     <param name="parse">false</param>
     ....
   </result>
 </action>

This enable result's parameter to be parsed with respect to Ognl's value stack. and returns its String representation

<action ...>
   <result ..>${protocol}${host}:${port}${contextPath}/xxxx/yyy.jsp</result>
 <action>

With an action having the following properties being in the stack :-

Property Value
protocol http://
host localhost
port 8080
contextPath /context

The "location" property of the above result will be effectively, http://localhost:8080/context/xxxx/yyy.jsp

The parsing is delegated to WebWork's TextParseUtil class

encode property and its relevant accessors

WebWorkResultSupport added an encode property which by default is false which could be activated by :-

<action ...>
  <result ...>
    <param name="encode">true</param>
    ....
  </result>
 </action>

This enable result's parameter to be encoded ( using URLEncoder.encode("....", "UTF-8") ) if it is set to true.

Result Types

Webwork provides several implementations of the com.opensymphony.xwork.Result interface to make web-based interactions with your actions simple. These result types include:

Results are specified in a xwork xml config file(xwork.xml) nested inside <action>. If the location param is the only param being specified in the result tag, you can simplify it as follows:

<action name="bar" class="myPackage.barAction">
  <result name="success" type="dispatcher">
    <param name="location">foo.jsp</param>
  </result>
</action>

or simplified

<action name="bar" class="myPackage.barAction">
  <result name="success" type="dispatcher">foo.jsp</result>
</action>

if you are extending webwork-default.xml, then the default result type is "dispatcher". Also, if you don't specify the name of a result, it is assumed to be "success". This means you can simply the result down to

<action name="bar" class="myPackage.barAction">
  <result>foo.jsp</result>
</action>

NOTE: The Parse attribute enables the location element to be parsed for expressions. An example of how this could be useful:

<result name="success" type="redirect">/displayCart.action?userId=${userId}</result>

NOTE: You can also specify global-results to use with multiple actions. This can save time from having to add the same result to many different actions. For more information on result tags and global-results, see Result Configuration section.